/*
 * Copyright (c) 2009-2020 Weasis Team and other contributors.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0, or the Apache
 * License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */
package org.weasis.base.ui.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import org.weasis.base.ui.Messages;
import org.weasis.core.api.gui.util.AppProperties;
import org.weasis.core.api.gui.util.JMVUtils;
import org.weasis.core.api.service.BundleTools;
import org.weasis.core.api.util.ResourceUtil;
import org.weasis.core.ui.util.SimpleTableModel;

public class WeasisAboutBox extends JDialog implements ActionListener {

  private final JPanel jpanelRoot = new JPanel();
  private final JPanel jPanelClose = new JPanel();
  private final JButton jButtonclose = new JButton();
  private final BorderLayout borderLayout1 = new BorderLayout();
  private JTable sysTable;
  private final JScrollPane jScrollPane1 = new JScrollPane();
  private final JTabbedPane jTabbedPane1 = new JTabbedPane();
  private final JPanel jPanelAbout = new JPanel();
  private final JPanel jPanelInfoSys = new JPanel();
  private final Border border1 = BorderFactory.createEmptyBorder(10, 10, 10, 10);
  private final FlowLayout flowLayout1 = new FlowLayout();
  private final BorderLayout borderLayout2 = new BorderLayout();
  private final GridBagLayout gridBagLayout1 = new GridBagLayout();

  private final JTextPane jTextPane1 = new JTextPane();

  private final JPanel jPanel3 = new JPanel();
  private final BorderLayout borderLayout3 = new BorderLayout();
  private final JLabel jLabel1 = new JLabel();
  private final JPanel jPanel1 = new JPanel();
  private final JScrollPane jScrollPane3 = new JScrollPane();

  public WeasisAboutBox(Frame owner) {
    super(
        owner,
        String.format(Messages.getString("WeasisAboutBox.about"), AppProperties.WEASIS_NAME),
        true);
    sysTable =
        new JTable(
            new SimpleTableModel(
                new String[] {
                  Messages.getString("WeasisAboutBox.prop"),
                  Messages.getString("WeasisAboutBox.val")
                },
                createSysInfo()));
    sysTable.getColumnModel().setColumnMargin(5);
    JMVUtils.formatTableHeaders(sysTable, SwingConstants.CENTER);
    init();
    pack();
  }

  private void init() {
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    this.setModal(true);
    jpanelRoot.setLayout(borderLayout1);
    jPanelClose.setLayout(flowLayout1);
    flowLayout1.setAlignment(FlowLayout.RIGHT);
    flowLayout1.setHgap(15);
    flowLayout1.setVgap(10);

    jButtonclose.setText(Messages.getString("WeasisAboutBox.close"));

    jButtonclose.addActionListener(this);
    jPanelInfoSys.setBorder(border1);
    jPanelInfoSys.setLayout(borderLayout2);

    jPanelAbout.setLayout(gridBagLayout1);
    jTextPane1.setEditorKit(JMVUtils.buildHTMLEditorKit(jTextPane1));
    jTextPane1.setContentType("text/html");
    jTextPane1.setEditable(false);

    jTextPane1.addHyperlinkListener(JMVUtils.buildHyperlinkListener());
    final StringBuilder message = new StringBuilder("<div align=\"center\"><H2>"); // NON-NLS
    message.append(AppProperties.WEASIS_NAME);
    message.append(" ");
    message.append(AppProperties.WEASIS_VERSION);
    message.append("</H2>"); // NON-NLS

    String rn = Messages.getString("WeasisWin.release");
    message.append(
        String.format(
            "<a href=\"%s", // NON-NLS
            BundleTools.SYSTEM_PREFERENCES.getProperty("weasis.releasenotes", ""))); // NON-NLS
    message.append("\" style=\"color:#FF9900\">"); // NON-NLS
    message.append(rn);
    message.append("</a>"); // NON-NLS
    message.append("<BR>");
    message.append(BundleTools.SYSTEM_PREFERENCES.getProperty("weasis.copyrights", "")); // NON-NLS
    message.append("</div>"); // NON-NLS
    jTextPane1.setText(message.toString());
    jLabel1.setBorder(BorderFactory.createLineBorder(Color.black, 2));

    jLabel1.setIcon(ResourceUtil.getLargeLogo());
    jPanel3.setLayout(borderLayout3);

    jTabbedPane1.add(jPanel3, this.getTitle());
    jPanel3.add(jPanel1, BorderLayout.NORTH);
    jPanel1.add(jLabel1, null);
    jPanel3.add(jScrollPane3, BorderLayout.CENTER);
    jScrollPane3.getViewport().add(jTextPane1, null);
    jTabbedPane1.add(jPanelInfoSys, Messages.getString("WeasisAboutBox.sys"));
    jPanelInfoSys.add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.setPreferredSize(new Dimension(320, 270));
    jScrollPane1.getViewport().add(sysTable, null);
    jPanelClose.add(jButtonclose, null);

    jpanelRoot.add(jPanelClose, BorderLayout.SOUTH);
    jpanelRoot.add(jTabbedPane1, BorderLayout.CENTER);

    this.getContentPane().add(jpanelRoot, null);
  }

  // Overridden so we can exit when window is closed
  @Override
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      cancel();
    }
    super.processWindowEvent(e);
  }

  void cancel() {
    dispose();
  }

  // Close the dialog on a button event
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jButtonclose) {
      cancel();
    }
  }

  private static Object[][] createSysInfo() {
    Properties sysProps = System.getProperties();
    Object[][] dataArray = new String[sysProps.size()][2];
    Enumeration<?> enumer = sysProps.propertyNames();
    for (int i = 0; i < dataArray.length; i++) {
      dataArray[i][0] = enumer.nextElement();
      dataArray[i][1] = sysProps.getProperty(dataArray[i][0].toString());
    }
    return dataArray;
  }
}
